#include "../../include/tool/configManage.h"
#include <string>
#include <thread>
#include <vector>

#include "../../include/tool/jsoncpp/json.h"
#include <iostream> //读写io c++标准库
#include <fstream> //读写文件 c++标准库
#include <string> //字符串类 c++标准库
#include <sstream> //字符串流 c++标准库


#include "../../include/tool/log.h"

using namespace std;

C_configManage::C_configManage(/* args */)
{
}

C_configManage::~C_configManage()
{
}

///不一定成功为什么是void?
void C_configManage::readFileJson(const string filename)
{
	Json::Reader C_jsonReader;
	Json::Value C_jsonJoot;
 
	//从文件中读取，保证当前文件有*.json文件  
	ifstream l_jsonFileStr(filename, ios::in);///配置文件是ascii为什么要二进制打开？
	if (!l_jsonFileStr.is_open()) {
		spdlog::error("Error opening file");
		return;
	}
 
	if (C_jsonReader.parse(l_jsonFileStr, C_jsonJoot)) {
		C_config_set.config_driver_en.config_imu_en = C_jsonJoot["config_driver_en"]["config_imu_en"].asBool();		
		C_config_set.config_driver_en.config_ultrasonic_en = C_jsonJoot["config_driver_en"]["config_ultrasonic_en"].asBool();	
		C_config_set.config_driver_en.config_gnss_en = C_jsonJoot["config_driver_en"]["config_gnss_en"].asBool();		
		C_config_set.config_driver_en.config_dtu_en = C_jsonJoot["config_driver_en"]["config_dtu_en"].asBool();		
		C_config_set.config_driver_en.config_bluetooth_en = C_jsonJoot["config_driver_en"]["config_bluetooth_en"].asBool();		
		C_config_set.config_driver_en.config_singlechip_en = C_jsonJoot["config_driver_en"]["config_singlechip_en"].asBool();
		C_config_set.config_driver_en.config_monochrome_camera_en = C_jsonJoot["config_driver_en"]["config_monochrome_camera_en"].asBool();
		C_config_set.config_driver_en.config_rgb_camera_en = C_jsonJoot["config_driver_en"]["config_rgb_camera_en"].asBool();
		
		spdlog::info( "config_imu_en:              '{}'", C_config_set.config_driver_en.config_imu_en);
		spdlog::info( "config_ultrasonic_en:       '{}'", C_config_set.config_driver_en.config_ultrasonic_en);
		spdlog::info( "config_gnss_en:             '{}'", C_config_set.config_driver_en.config_gnss_en);
		spdlog::info( "config_dtu_en:              '{}'", C_config_set.config_driver_en.config_dtu_en);
		spdlog::info( "config_bluetooth_en:        '{}'",  C_config_set.config_driver_en.config_bluetooth_en);
		spdlog::info( "config_singlechip_en:       '{}'", C_config_set.config_driver_en.config_singlechip_en);
		spdlog::info( "config_monochrome_camera_en:'{}'", C_config_set.config_driver_en.config_monochrome_camera_en);
		spdlog::info( "config_rgb_camera_en:       '{}'", C_config_set.config_driver_en.config_rgb_camera_en);

		/* 获取DTU的配置 */
		C_config_set.config_driver.config_dtu.config_task.task_name = C_jsonJoot["config_driver"]["config_DTU"]["config_task"]["task_name"].asString();
		C_config_set.config_driver.config_dtu.config_task.task_period = C_jsonJoot["config_driver"]["config_DTU"]["config_task"]["task_period"].asInt();
		C_config_set.config_driver.config_dtu.config_task.task_topic = C_jsonJoot["config_driver"]["config_DTU"]["config_task"]["task_topic"].asString();
		C_config_set.config_driver.config_dtu.config_serial_hardware.serial_device = C_jsonJoot["config_driver"]["config_DTU"]["config_hardware"]["serial_device"].asString();
		C_config_set.config_driver.config_dtu.config_serial_hardware.baud = C_jsonJoot["config_driver"]["config_DTU"]["config_hardware"]["baud"].asInt();
		C_config_set.config_driver.config_dtu.config_serial_hardware.data_bits = C_jsonJoot["config_driver"]["config_DTU"]["config_hardware"]["data_bits"].asInt();
		C_config_set.config_driver.config_dtu.config_serial_hardware.stop_bits = C_jsonJoot["config_driver"]["config_DTU"]["config_hardware"]["stop_bits"].asInt();
		/* 获取RTK的配置 */
		C_config_set.config_driver.config_gnss.config_task.task_name = C_jsonJoot["config_driver"]["config_gnss"]["config_task"]["task_name"].asString();
		C_config_set.config_driver.config_gnss.config_task.task_period = C_jsonJoot["config_driver"]["config_gnss"]["config_task"]["task_period"].asInt();
		C_config_set.config_driver.config_gnss.config_task.task_topic = C_jsonJoot["config_driver"]["config_gnss"]["config_task"]["task_topic"].asString();
		C_config_set.config_driver.config_gnss.config_serial_hardware.serial_device = C_jsonJoot["config_driver"]["config_gnss"]["config_hardware"]["serial_device"].asString();
		C_config_set.config_driver.config_gnss.config_serial_hardware.baud = C_jsonJoot["config_driver"]["config_gnss"]["config_hardware"]["baud"].asInt();
		C_config_set.config_driver.config_gnss.config_serial_hardware.data_bits = C_jsonJoot["config_driver"]["config_gnss"]["config_hardware"]["data_bits"].asInt();
		C_config_set.config_driver.config_gnss.config_serial_hardware.stop_bits = C_jsonJoot["config_driver"]["config_gnss"]["config_hardware"]["stop_bits"].asInt();
		/* 获取单片机的配置 */
		C_config_set.config_driver.config_singchip.config_task.task_name = C_jsonJoot["config_driver"]["config_singchip"]["config_task"]["task_name"].asString();
		C_config_set.config_driver.config_singchip.config_task.task_period = C_jsonJoot["config_driver"]["config_singchip"]["config_task"]["task_period"].asInt();
		C_config_set.config_driver.config_singchip.config_task.task_topic = C_jsonJoot["config_driver"]["config_singchip"]["config_task"]["task_topic"].asString();
		C_config_set.config_driver.config_singchip.config_serial_hardware.serial_device = C_jsonJoot["config_driver"]["config_singchip"]["config_hardware"]["serial_device"].asString();
		C_config_set.config_driver.config_singchip.config_serial_hardware.baud = C_jsonJoot["config_driver"]["config_singchip"]["config_hardware"]["baud"].asInt();
		C_config_set.config_driver.config_singchip.config_serial_hardware.data_bits = C_jsonJoot["config_driver"]["config_singchip"]["config_hardware"]["data_bits"].asInt();
		C_config_set.config_driver.config_singchip.config_serial_hardware.stop_bits = C_jsonJoot["config_driver"]["config_singchip"]["config_hardware"]["stop_bits"].asInt();
		/* 获取IMU的配置 */
		C_config_set.config_driver.config_imu.config_task.task_name = C_jsonJoot["config_driver"]["config_imu"]["config_task"]["task_name"].asString();
		C_config_set.config_driver.config_imu.config_task.task_period = C_jsonJoot["config_driver"]["config_imu"]["config_task"]["task_period"].asInt();
		C_config_set.config_driver.config_imu.config_task.task_topic = C_jsonJoot["config_driver"]["config_imu"]["config_task"]["task_topic"].asString();
		C_config_set.config_driver.config_imu.config_serial_hardware.serial_device = C_jsonJoot["config_driver"]["config_imu"]["config_hardware"]["serial_device"].asString();
		C_config_set.config_driver.config_imu.config_serial_hardware.baud = C_jsonJoot["config_driver"]["config_imu"]["config_hardware"]["baud"].asInt();
		C_config_set.config_driver.config_imu.config_serial_hardware.data_bits = C_jsonJoot["config_driver"]["config_imu"]["config_hardware"]["data_bits"].asInt();
		C_config_set.config_driver.config_imu.config_serial_hardware.stop_bits = C_jsonJoot["config_driver"]["config_imu"]["config_hardware"]["stop_bits"].asInt();
		
		spdlog::info("Reading Complete!");
	}
	else {
		spdlog::error("parse error");
	}
 
	l_jsonFileStr.close();
}

int32_t C_configManage::getAllConfig(const std::string filePath)
{
	spdlog::info("read json path: '{}'", filePath);
    if(filePath.length() == 0){
        return false;
    }
	readFileJson(filePath);
	return true; ///读失败了还是返回true? return readFileJson(filePath);
}